import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/internal/operators';
import queryString from 'query-string';
import { API_CONFIG, ServicesModule } from './services.module';
import { SampleBack, SongSheet } from '@services/data-types/common.type';
import { Signin, User, QRKey, QRData, QRStatus, UserSheet, RecordVal } from '@services/data-types/member.type';
import { LoginParams } from '@share/wyy-ui/wyy-layer/wyy-layer-login/wyy-layer-login.component';

export enum RecordType {
  allData,
  weekData
}

export interface LikeSongParams {
  pid: string;
  tracks: string;
}

export interface ShareParams {
  id: string;
  msg: string;
  type: string;
}

@Injectable({
  providedIn: ServicesModule
})
export class MemberService {

  constructor(private http: HttpClient, @Inject(API_CONFIG) private url: string) { }
  // 手机号登录
  login(formValue: LoginParams): Observable<User> {
    const params = new HttpParams({ fromString: queryString.stringify(formValue) });
    return this.http.get(this.url + '/login/cellphone', { params })
      .pipe(map((res: User) => res));
  }
  // 获取登录用户的详细信息
  getUserDetail(uid: string): Observable<User> {
    return this.http.get(this.url + `/user/detail?uid=${uid}`)
      .pipe(map((res: User) => res));
  }
  // 二维码key
  getQRKey(timestamp: number): Observable<QRKey> {
    const params = new HttpParams({ fromString: queryString.stringify(timestamp) });
    return this.http.get(this.url + '/login/qr/key', { params })
      .pipe(map((res: { data: QRKey }) => res.data));
  }
  // 生成二维码
  getQR(key: string, timestamp: number): Observable<QRData> {
    return this.http.get(this.url + `/login/qr/create?key=${key}&qrimg=true&timestamp=${timestamp}`)
      .pipe(map((res: { data: QRData }) => res.data));
  }
  // 检测扫码状态
  checkQR(key: string, timestamp: number): Observable<QRStatus> {
    return this.http.get(this.url + '/login/qr/check?key=' + key + '&timestamp=' + timestamp + '&noCookie=true')
      .pipe(map((res: QRStatus) => res));
  }
  // 获取账号信息
  getAccountInfo(): Observable<User> {
    return this.http.get(this.url + '/user/account')
      .pipe(map((res: User) => res));
  }
  // 用户签到
  signIn(): Observable<Signin> {
    const params = new HttpParams({ fromString: queryString.stringify({ type: 1 } ) });
    return this.http.get(this.url + '/daily_signin', { params })
      .pipe(map((res: Signin) => res));
  }
  // 听歌记录
  getUserRecord(uid: string, type = RecordType.weekData): Observable<RecordVal[]> {
    const params = new HttpParams({ fromString: queryString.stringify({ uid, type } ) });
    return this.http.get(this.url + '/user/record', { params })
      .pipe(map((res: RecordVal) => res[RecordType[type]]));
  }
  // 用户歌单
  getUserSheets(uid: string): Observable<UserSheet> {
    const params = new HttpParams({ fromString: queryString.stringify({ uid } ) });
    return this.http.get(this.url + '/user/playlist', { params })
      .pipe(map((res: { playlist: SongSheet[] }) => {
        const list = res.playlist;
        return {
          self: list.filter(item => !item.subscribed),
          subscribed: list.filter(item => item.subscribed)
        };
      }));
  }
  // 收藏歌曲
  likeSong({ pid, tracks }: LikeSongParams): Observable<number> {
    const params = new HttpParams({ fromString: queryString.stringify({ pid, tracks, op: 'add' }) });
    return this.http.get(this.url + '/playlist/tracks', { params }).pipe(map((res: SampleBack) => res.code));
  }
  // 收藏歌单
  likeSongSheet(id: string, t = 1): Observable<number> {
    const params = new HttpParams({ fromString: queryString.stringify({ id, t }) });
    return this.http.get(this.url + '/playlist/subscribe', { params }).pipe(map((res: SampleBack) => res.code));
  }
  // 收藏歌手
  likeSinger(id: string, t = 1): Observable<number> {
    const params = new HttpParams({ fromString: queryString.stringify({ id, t }) });
    return this.http.get(this.url + '/artist/sub', { params }).pipe(map((res: SampleBack) => res.code));
  }
  // 创建歌单
  createSheet(name: string): Observable<string> {
    const params = new HttpParams({ fromString: queryString.stringify({ name }) });
    return this.http.get(this.url + '/playlist/create', { params }).pipe(map((res: SampleBack) => res.id.toString()));
  }
  // 分享
  shareResource({id, msg, type = 'song'}: ShareParams): Observable<number> {
    const params = new HttpParams({ fromString: queryString.stringify({ id, msg, type }) });
    return this.http.get(this.url + '/share/resource', { params }).pipe(map((res: SampleBack) => res.code));
  }
  // 发送验证码
  sendSmsCode(phone: number): Observable<number> {
    const params = new HttpParams({ fromString: queryString.stringify({ phone }) });
    return this.http.get(this.url + '/captcha/sent', { params }).pipe(map((res: SampleBack) => res.code));
  }
  // 校验验证码
  checkSmsCode(phone: number, captcha: number): Observable<number> {
    const params = new HttpParams({ fromString: queryString.stringify({ phone, captcha }) });
    return this.http.get(this.url + '/captcha/verify', { params }).pipe(map((res: SampleBack) => res.code));
  }
  // 验证当前手机号是否已注册
  checkPhoneExists(phone: number) {
    const params = new HttpParams({ fromString: queryString.stringify({ phone }) });
    return this.http.get(this.url + '/cellphone/existence/check', { params }).pipe(map((res: { exist: number }) => res.exist));
  }
  // 退出登录
  logout(): Observable<SampleBack> {
    return this.http.get(this.url + '/logout').pipe(map(res => res as SampleBack));
  }
}
